﻿using System;
using System.Collections.Generic;
using SysDraw = System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
using SharpDX;

namespace SFSpacefight
{
	public static class Methods
	{
		public static List<List<Bitmap>> shipbitmaps;//0 = playership 
		public static List<ShipInfo> shipinfos;
		
		public static void load(RenderTarget target)
		{
			List<Bitmap> playerbitmaps = new List<Bitmap>();
			shipbitmaps = new List<List<Bitmap>>();

			//for (int i = 0; i < 73; i++)
			//{
			//	String filename = Environment.CurrentDirectory + "\\Images\\1-" + i.ToString().PadLeft(2, '0') + ".png";
			//	SysDraw.Bitmap bitmap = (SysDraw.Bitmap)SysDraw.Bitmap.FromFile(filename);

			//	playerbitmaps.Add(LoadBitmap(target, bitmap));
			//}

			shipbitmaps.Add(playerbitmaps);
			LoadShipInfo();
		}
		private static Bitmap LoadBitmap(RenderTarget target, SysDraw.Bitmap drawingBitmap)
		{
			Bitmap result = null;

			//Lock the gdi resource
			SysDraw.Imaging.BitmapData drawingBitmapData = drawingBitmap.LockBits(
				new SysDraw.Rectangle(0, 0, drawingBitmap.Width, drawingBitmap.Height),
				SysDraw.Imaging.ImageLockMode.ReadOnly, SysDraw.Imaging.PixelFormat.Format32bppPArgb);

			//Prepare loading the image from gdi resource
			DataStream dataStream = new DataStream(drawingBitmapData.Scan0, drawingBitmapData.Stride * drawingBitmapData.Height, true, false);
			BitmapProperties properties = new BitmapProperties();
			properties.PixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);

			//Load the image from the gdi resource
			result = new Bitmap(target, new DrawingSize(drawingBitmap.Width, drawingBitmap.Height), dataStream, drawingBitmapData.Stride, properties);

			//Unlock the gdi resource
			drawingBitmap.UnlockBits(drawingBitmapData);

			return result;
		}
		private static void LoadShipInfo()
		{
			shipinfos = new List<ShipInfo>();
			shipinfos.Add(new ShipInfo() 
			{ 
				name = "Old Scout",
				description = "a rusty, worn out ship",
				mass = 50,
				thrust = 190,
				speed = 40, 
				upgradeslots = 6,
				combatslots = 3
			});
		}

		public static float smoothstep(float p0, float m0, float p1, float m1, double t)// 1st point, 1st speed, 2nd point, 2nd speed, time = [0, 1]
		{
			double t2, t3; //greatest double accuracy loss occurs here

			t2 = t * t;
			t3 = t2 * t;

			return (float)((2 * t3 - 3 * t2 + 1) * p0 + (t3 - 2 * t2 + t) * m0 + (-2 * t3 + 3 * t2) * p1 + (t3 - t2) * m1);
		}
		public static double smoothstep(double p0, double m0, double p1, double m1, double t)// 1st point, 1st speed, 2nd point, 2nd speed, time = [0, 1]
		{
			double t2, t3; //greatest double accuracy loss occurs here

			t2 = t * t;
			t3 = t2 * t;

			return ((2 * t3 - 3 * t2 + 1) * p0 + (t3 - 2 * t2 + t) * m0 + (-2 * t3 + 3 * t2) * p1 + (t3 - t2) * m1);
		}
		public static double linstep(double y0, double y1, double percent)
		{
			return y0 + (y1 - y0) * percent;
		}
		public static float linstep(float y0, float y1, double percent)
		{
			return (float)(y0 + (y1 - y0) * percent);
		}
	}
}
